/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

public class Count {

	String role = null;
	int count = 0;
	private String category = null;
	private boolean pure = true;

	public Count() {
		super();
	}  
 

	public Count(Count template) {
		super();
		this.role = template.role;
		this.count = template.count;
		this.category = template.category;
		this.pure = template.pure;
	}  
 
	public Count(String key, int value, String category, boolean isPure) {
		super();
		if (key == null) this.role = null;
		else this.role = key.trim();
		this.count = value;
		if (category == null || category.isEmpty()) this.category = null;
		else this.category = category.trim();
		this.pure = isPure;
	}  
 
	/**
	 * @return the value
	 */
	@XmlValue
	public int getCount() {
		return this.count;
	}
	/**
	 * @return the key
	 */
	@XmlAttribute(required = true)
	public String getRole() {
		if (role == null) return null;
		else return this.role;
	}
	/**
	 * @return the key
	 */
	@XmlAttribute(required = false)
	public String getCategory() {
		if (category == null) return null;
		else return this.category.toString();
	}
	/**
	 * @param value the value to set
	 */
	public void setCount(int value) {
		this.count = value;
	}
	/**
	 * @param key the key to set
	 */
	public void setRole(String key) {
		if (key == null) this.role = null;
		else this.role = key.trim();
	}
	/**
	 * @param key the key to set
	 */
	public void setCategory(String key) {
		if (key == null || key.isEmpty()) this.category = null;
		else this.category = key.trim();
	}

	public void mergeCategory(Count newCount) {
		// Merge categories: if one is null or they are different, the result is null.
		String newCategory = newCount.getCategory();
		if (newCategory==null) {
			this.category = null;
		}
		else if ( ! newCategory.equals(this.category)) {
			this.category = null;
		} else if ( ! newCount.isPure() ) {
			this.pure = false;
		}
	}
	
	public String toString() {
		return role+"="+count+" "+(category==null?"*":category)+(pure?"!":"");
	}


	/**
	 * @return the pure
	 */
	@XmlAttribute(required = false)
	public boolean isPure() {
		return pure;
	}


	/**
	 * @param pure the pure to set
	 */
	public void setPure(boolean pure) {
		this.pure = pure;
	}
}
